define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'consumer/index' + location.search,
                    add_url: 'consumer/add',
                    edit_url: 'consumer/edit',
                    del_url: 'consumer/del',
                    multi_url: 'consumer/multi',
                    table: 'consumer',
                }
            });

            var table = $("#table");

            // 修改表格搜索框的默认placeholder
            $.fn.bootstrapTable.locales[Table.defaults.locale]['formatSearch'] = function(){return "经销商,微信名,抖音名";};

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showToggle: false,
                searchFormVisible: true,
                exportTypes: ['csv','excel'],
                clickToSelect: false,
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name')},
                        {field: 'phone', title: __('Phone')},
                        {field: 'wechatname', title: __('Wechatname')},
                        {
                            field: 'headavatar',
                            title: __('Headavatar'),
                            operate: false,
                            formatter: function (value, row, index) {
                                if(value){
                                    return '<a href="'+value+'" target="_blank"><img src="'+value+'" width="60px"></a>'
                                }
                            }
                        },
                        {field: 'douyinname', title: __('Douyinname')},
                        {field: 'firstlogin', title: __('Firstlogin'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.dateonly},
                        // {field: 'dealer_id', title: __('Dealer_id')},
                        {
                            field: 'ordernum',
                            title: __('Ordernum'),
                            operate: false
                        },
                        {
                            field: 'total',
                            title: __('Total'),
                            formatter: function (value, row, index) {
                                return '¥ '+value;
                            },
                            operate: false
                        },
                        {field: 'dealer.name', title: __('Dealer'), operate: false},
                        {
                            field: 'is_dealer',
                            title: __('Is_dealer'),
                            operate: false,
                            formatter: function (value, row, index) {
                                if(value == 1) {
                                    return '是';
                                }else {
                                    return '否';
                                }
                            }
                        },
                        {
                            field: 'type',
                            title: __('Type'),
                            searchList: {'1':__('Type 1'),'2':__('Type 2')},
                            formatter: Table.api.formatter.status
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            buttons:[
                                {
                                    name: 'consumerdetail',
                                    text: __('Consumerdetail'),
                                    classname: 'btn btn-success btn-dialog',
                                    url: 'consumer/detail',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                },
                                {
                                    name: 'edit',
                                    text: __('Edit'),
                                    classname: 'btn btn-info btn-dialog',
                                    url: 'consumer/edit',
                                },
                                {
                                    name: 'del',
                                    text: __('Del'),
                                    classname: 'btn btn-primary btn-ajax',
                                    url: 'consumer/del',
                                    confirm: '确认删除?',
                                    refresh: true,
                                },
                            ],
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);

                                // 隐藏编辑和删除按钮
                                // $(table).data("operate-edit", null);
                                // $(table).data("operate-del", null);

                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'consumer/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'consumer/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'consumer/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };

    return Controller;
});